package cc.comac.mom.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.comac.mom.entity.QualityAttachmentEntity;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import cc.comac.mom.api.EquipRepairRepositoryRepairDocService;
import cc.comac.mom.bean.EquipRepairRepositoryRepairDoc;
import cc.comac.mom.config.OSSService;
import cc.comac.mom.config.UploadPreviewData;
import cc.comac.mom.util.ResultBean;

@RestController
@RequestMapping("/Equip/repair/repository/RepairDoc")
public class EquipRepairRepositoryRepairDocController {

	@Autowired
	private EquipRepairRepositoryRepairDocService equipRepairRepositoryRepairDocService;
	@Autowired
	private OSSService oSSService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ResultBean save(@RequestBody EquipRepairRepositoryRepairDoc obj) {
		return equipRepairRepositoryRepairDocService.save(obj);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ResultBean delete(@RequestBody EquipRepairRepositoryRepairDoc obj) {
		return equipRepairRepositoryRepairDocService.delete(obj);
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResultBean update(@RequestBody EquipRepairRepositoryRepairDoc obj) {
		return equipRepairRepositoryRepairDocService.update(obj);
	}

	@RequestMapping(value = "/saveBatch", method = RequestMethod.POST)
	public ResultBean saveBatch(@RequestBody List<EquipRepairRepositoryRepairDoc> list) {

		return equipRepairRepositoryRepairDocService.save(list);
	}

	@RequestMapping(value = "/updateBatch", method = RequestMethod.POST)
	public ResultBean updateBatch(@RequestBody List<EquipRepairRepositoryRepairDoc> list) {
		return equipRepairRepositoryRepairDocService.update(list);
	}

	@RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
	public ResultBean deleteBatch(@RequestBody List<EquipRepairRepositoryRepairDoc> list) {
		return equipRepairRepositoryRepairDocService.delete(list);
	}

	@RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
	public EquipRepairRepositoryRepairDoc getById(@PathVariable long id) {
		EquipRepairRepositoryRepairDoc equipRepairRepositoryRepairDoc = equipRepairRepositoryRepairDocService.getById(id);
			return equipRepairRepositoryRepairDoc;
	}

	@GetMapping(value = "/previewPdf")
	public ResponseEntity<Object> previewPdf(HttpServletRequest request, HttpServletResponse response,
											 @RequestParam long repairDocId)  {
		EquipRepairRepositoryRepairDoc equipRepairRepositoryRepairDoc = equipRepairRepositoryRepairDocService.getById(repairDocId);
		try {
			// 以流的形式下载文件。
			InputStream fis=oSSService.getFileByPathFromOss(equipRepairRepositoryRepairDoc.getInnerData().getDoucumentRealPath());

			ByteArrayResource bar = new ByteArrayResource(IOUtils.toByteArray(fis));

			return ResponseEntity.ok()
					.header("Content-Disposition", "inline;filename=" +equipRepairRepositoryRepairDoc.getInnerData().getDocumentName() + "")
					.contentType(MediaType.APPLICATION_PDF)
					.contentLength(bar.contentLength())
					.body(bar);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/download/{documentId}")
	public ResultBean download(@PathVariable("documentId") Long documentId, HttpServletResponse response) {
		EquipRepairRepositoryRepairDoc equipRepairRepositoryRepairDoc = equipRepairRepositoryRepairDocService.getById(documentId);
			try {
				oSSService.downFileFromOSS(response,
						equipRepairRepositoryRepairDoc.getInnerData().getDoucumentRealPath(),
						equipRepairRepositoryRepairDoc.getInnerData().getDocumentName());
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}

			// UploadPreviewData.download(equipRepairRepositoryRepairDoc.getInnerData().getDoucumentRealPath(),
			// equipRepairRepositoryRepairDoc.getInnerData().getDocumentName(), response);
		return null;
	}

	@RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
	public ResultBean selectByCondition(@RequestBody EquipRepairRepositoryRepairDoc obj) {
		return equipRepairRepositoryRepairDocService.selectByCondition(obj);
	}

}
